[group:dhcpmon]
programs=
{%- set add_preceding_comma = { 'flag': False } %}
{% set vlan_list = [] %}
{# Go through Vlan interfaces, if not ipv4 address or dhcpv4 server configured, then do not generate dhcpmon for them #}
{% for (vlan_name, prefix) in VLAN_INTERFACE | pfx_filter %}
    {%- if prefix | ipv4 and VLAN and vlan_name in VLAN and 'dhcp_servers' in VLAN[vlan_name] and VLAN[vlan_name]['dhcp_servers']|length > 0 %}
        {%- for dhcp_server in VLAN[vlan_name]['dhcp_servers'] %}
            {%- if dhcp_server | ipv4 %}
                {%- set _ = vlan_list.append(vlan_name) %}
            {%- endif %}
        {%- endfor %}
    {%- endif %}
{% endfor %}
{% for vlan_name in vlan_list | unique %}
    {%- if add_preceding_comma.flag %},{% endif %}
        {%- set _dummy = add_preceding_comma.update({'flag': True}) %}
dhcpmon-{{ vlan_name }}
{%- endfor%}


{% for vlan_name in vlan_list | unique %}
[program:dhcpmon-{{ vlan_name }}]
{# We treat this VLAN as a downstream interface (-id), as we only want to listen for requests #}
command=/usr/sbin/dhcpmon -id {{ vlan_name }}
{#- Dual ToR Option #}
{% if 'subtype' in DEVICE_METADATA['localhost'] and DEVICE_METADATA['localhost']['subtype'] == 'DualToR' %} -u Loopback0{% endif -%}
{#- We treat all other interfaces as upstream interfaces (-iu), as we only want to listen for replies #}
{% for (name, prefix) in VLAN_INTERFACE|pfx_filter %}
{% if prefix | ipv4 and name != vlan_name %} -iu {{ name }}{% endif -%}
{% endfor %}
{% for (name, prefix) in INTERFACE|pfx_filter %}
{% if prefix | ipv4 %} -iu {{ name }}{% endif -%}
{% endfor %}
{% for (name, prefix) in PORTCHANNEL_INTERFACE|pfx_filter %}
{% if prefix | ipv4 %} -iu {{ name }}{% endif -%}
{% endfor %}
{% if MGMT_INTERFACE %}
{% for (name, prefix) in MGMT_INTERFACE|pfx_filter %}
{% if prefix | ipv4 %} -im {{ name }}{% endif -%}
{% endfor %}
{% endif %}

priority=4
autostart=false
autorestart=false
stdout_logfile=NONE
stdout_syslog=true
stderr_logfile=NONE
stderr_syslog=true
dependent_startup=true
dependent_startup_wait_for=isc-dhcpv4-relay-{{ vlan_name }}:running

{% endfor %}
